package android.slc.extras.component.attchment.ui.vd;

import android.os.Bundle;
import android.slc.extras.component.attchment.domain.AttachmentEditVmBox;
import android.slc.extras.component.attchment.domain.AttachmentReviewVmBox;
import android.slc.extras.component.attchment.utils.AttachmentUtils;
import android.slc.mp.SlcMp;
import android.slc.mp.SlcMpConfig;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.lifecycle.LifecycleOwner;

import com.blankj.utilcode.util.ActivityUtils;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/8/10 11:26
 */
public class AttachmentEditVd<VM extends AttachmentEditVmBox<A>, A extends AttachmentReviewVmBox.AttachmentAdapter> extends AttachmentBaseVd<VM, A> {

    protected ActivityResultLauncher<Bundle> pickerActivityResultLauncher;

    @Override
    protected void initView() {
        super.initView();
        if (pickerActivityResultLauncher == null) {
            pickerActivityResultLauncher = SlcMp.getInstance().registerPicker(activityResultCaller, vmBox.selectAttEventResultCallback);
        }
    }

    @Override
    protected void initMustCondition() {
        super.initMustCondition();
        if (activityResultCaller instanceof LifecycleOwner) {
            vmBox.getSelectAttEvent().observe((LifecycleOwner) activityResultCaller, unused -> onSelectAtt());
        }
    }

    /**
     * 选择附件
     */
    protected void onSelectAtt() {
        SlcMp.getInstance()
                .with((ComponentActivity) ActivityUtils.getTopActivity())
                .setMpConfig(new SlcMpConfig
                        .Builder()
                        .loadPhoto()
                        .loadAudio()
                        .loadVideo()
                        .loadFile(AttachmentUtils.TYPE_WORD, AttachmentUtils.TYPE_NAME_WORD, AttachmentUtils.Word)
                        .loadFile(AttachmentUtils.TYPE_EXCEL, AttachmentUtils.TYPE_NAME_EXCEL, AttachmentUtils.Excel)
                        .loadFile(AttachmentUtils.TYPE_PPT, AttachmentUtils.TYPE_NAME_PPT, AttachmentUtils.Ppt)
                        .loadFile(AttachmentUtils.TYPE_PDF, AttachmentUtils.TYPE_NAME_PDF, AttachmentUtils.Pdf)
                        .loadFile(AttachmentUtils.TYPE_COMPRESSION, AttachmentUtils.TYPE_NAME_COMPRESSION, AttachmentUtils.Compression)
                        .setMaxPicker(vmBox.getMaxCount() - vmBox.getDataList().size())
                        .build())
                .setPickerActivityResultLauncher(pickerActivityResultLauncher)
                .build();
    }

    /*@Override
    protected void onAdapterItemClick(@NonNull BaseQuickAdapter<Attachment, BaseViewHolder> adapter, @NonNull View view, int position) {
        super.onAdapterItemClick(adapter, view, position);
        Attachment attachment = adapter.getItem(position);
        Progress progress = attachment.getProgress();
        if (progress == null || attachment.isLocalBody() || progress.status != Progress.WAITING && progress.status != Progress.LOADING) {
            vmBox.onPreview(attachment);
        } else if (attachment.isNetBody()) {
            ToastUtils.showShort(R.string.label_please_wait_for_the_download_to_complete);
        }
    }*/

}
